home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-04 | 10.7 KB | 374 lines | [TEXT/KAHL] |
- /********************************************************* DEFINITION
- DATE: 9/26/93
- AUTHOR: Eric R. Rosé
-
- CLASS: CPPYenta
-
- SUPERCLASS: CPPApplication
-
- This C++ class defines the backbone for the Yenta Application.
-
- ********************************************************************/
-
- #include "CPPYenta.h"
- #include "CPPSendWindow.h"
- #include <CPPTaskManager.h>
- #include "CPPMessageWindow.h"
- #include "CPPYentaMenuBar.h"
- #include "CPPPrefsWindow.h"
- #include "CPPYentaAboutBox.h"
- #include "MyCommands.h"
- #include <CPPWindowManager.h>
- #include <CPPMaBell.h>
- #include <CPPZone411.h>
- #include <CPPNode411.h>
- #include <CPPNodeInfo.h>
- #include "CPPSpawnConfirmTask.h"
- #include "CPPSpawnZoneTask.h"
- #include "CPPYListenTask.h"
- #include <Commands.h>
- #include <CPPSound.h>
- #include <CPPDRequest.h>
- #include <Script.h>
- #include <StringTools.h>
- #include <ToolboxTools.h>
- #include <MemoryTools.h>
-
-
- #include <CPPStringList.h>
- #include <CPPObjectList.h>
- #include <CPPList.h>
- #include <PPCToolbox.h>
-
- /*-----------------------------------------------------------------*/
- /*--------------------- APPLICATION GLOBALS -----------------------*/
- /*-----------------------------------------------------------------*/
-
- CPPNodeInfo *gOurIdentity;
- CPPZoneList *gZoneList; // pointer to the send dialog's zone list
- CPPUserList *gUserList; // pointer to the send dialog's user list
- CPPList *gTalkText; // incoming message queue
- CPPSpawnConfirmTask *gStartConfirmUsersTask = NULL;
- CPPSpawnZoneTask *gStartScanZoneTask = NULL;
- PPCPortRefNum gOurPort;
- CPPSound *gIncomingSound;
- CPPSound *gLogonSound;
- CPPMaBell *gMaBell;
- CPPTaskManager *gSlaveDriver;
- PrefsData gPrefsInfo = {0, 0, TRUE, TRUE};
- StringPtr gReplyString = NULL;
- CPPSendWindow *gSendWindow;
- CPPMessageWindow *gMessageWindow;
- StringPtr gZoneName = NULL;
- StringPtr gAppName = "\pYenta";
-
- extern CPPMenuBar *gMenuBar;
- extern CPPWindowManager *gWindowManager;
-
- /*-----------------------------------------------------------------*/
- /*--------------------- FORWARD DECLARATIONS ----------------------*/
- /*-----------------------------------------------------------------*/
-
- void RecordOurAddress (CPPObject *TheObject);
- void SetStatusMessage (StringPtr NewMessage, Boolean MakeCopy);
- StringPtr ShortName (CPPNodeInfo *theNode);
-
- /*-----------------------------------------------------------------*/
- /*-----------------------------------------------------------------*/
- /*-----------------------------------------------------------------*/
-
- void SetStatusMessage (StringPtr NewMessage, Boolean MakeCopy)
- /* set the message in the send window's status box */
- {
- gSendWindow->SetStatusMessage (NewMessage, MakeCopy);
- }
-
- /*-----------------------------------------------------------------*/
-
- void RecordOurAddress (CPPObject *TheObject)
- /* use this completion routine to store our address */
- {
- CPPNode411 *TheTask = (CPPNode411 *)TheObject;
-
- gOurIdentity = TheTask->NthNode(1, TRUE);
- }
-
- /*-----------------------------------------------------------------*/
-
- StringPtr ShortName (CPPNodeInfo *theNode)
- /* construct a string of the form Name@zone for the given node */
- {
- StringPtr ObjStr, TypeStr, ZoneStr;
- Str255 STemp;
- Str32 NewTypeName;
- short i = 1;
-
- if (theNode)
- {
- theNode->GetNodeName (&ObjStr, &TypeStr, &ZoneStr);
- while (((TypeStr[i] & 0x00FF) != 0xA5) && (i <= TypeStr[0]))
- i++;
- CopyString(TypeStr, NewTypeName);
- NewTypeName[0] = i-1;
- PStrCat (255, STemp, 3, NewTypeName, "\p@", ZoneStr);
- return String2String (STemp);
- }
- else
- return NULL;
- }
-
- /*-----------------------------------------------------------------*/
- /*------------------------ PUBLIC METHODS -------------------------*/
- /*-----------------------------------------------------------------*/
-
- CPPYenta::CPPYenta (void) : CPPApplication ()
- {
-
- }
-
- /*-----------------------------------------------------------------*/
-
- CPPYenta::~CPPYenta (void)
- {
-
- }
-
- /*-----------------------------------------------------------------*/
-
- char *CPPYenta::ClassName (void)
- {
- return "CPPYenta";
- }
-
- /*-----------------------------------------------------------------*/
- /*----------------------- PROTECTED METHODS -----------------------*/
- /*-----------------------------------------------------------------*/
-
- void CPPYenta::BuildMenuBar (void)
- /* create the application menu bar */
- {
- gMenuBar = new CPPYentaMenuBar ();
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPYenta::DoApplicationSetup(void)
- /* initialize application globals */
- {
- StringPtr ObjString = NULL;
- short MyNodeID, MyZoneID;
- CPPZone411 *ZoneLookup;
- CPPYListenTask *LTask;
- CPPNode411 *NodeLookup;
- Str32 TypeName;
- OSErr ErrCode;
-
- this->idleTime = 15; // set background processing time
-
- if (!DoRequest("\pEnter the name you want to be known by; no more than 30 characters, please",
- GetChooserName("\pThe Lone Ranger"), &ObjString))
- ExitToShell();
-
- if (*ObjString > 30)
- *ObjString = 30;
-
- // create the appletalk manager and open the needed drivers
- gMaBell = new CPPMaBell (TRUE);
- if ((ErrCode = gMaBell->InitPPCToolbox()) != noErr)
- {
- ErrorAlert (ErrCode, NULL);
- ExitToShell();
- }
- // create the lookup/read/write task manager
- gSlaveDriver = new CPPTaskManager();
-
- // create a ZoneLookup task to find out what zone we are on
- ZoneLookup = new CPPZone411(gSlaveDriver, gMaBell, 17, TRUE);
- gZoneName = ZoneLookup->GetOurZoneName(&ErrCode);
- delete ZoneLookup;
-
- // find out where we are on the network
- if ((ErrCode = GetNodeAddress(&MyNodeID, &MyZoneID)) != noErr)
- {
- ErrorAlert (ErrCode, "\pCan't get our network address.");
- ExitToShell();
- }
-
- // open the port we will use to communicate through
- if ((ErrCode = gMaBell->OpenCommunicationPort (ObjString,
- gAppName,
- &gOurPort)) != noErr)
- {
- ErrorAlert (ErrCode, "\pCan't open a port to communicate with.");
- ExitToShell();
- }
-
- // now that we have opened our communications port, post a 411
- // node lookup task to get our full address
- PStrCat(32, TypeName, 3, ObjString, "\p•", gAppName);
- NodeLookup = new CPPNode411(gSlaveDriver, 5, TRUE);
- NodeLookup->StartNodeLookup ("\p=", TypeName, gZoneName, 1,
- RecordOurAddress);
-
- // having opened a port, set up a ConnectionTask to handle
- // communications on that port
- LTask = new CPPYListenTask(gSlaveDriver, 60, TRUE);
- LTask->StartListenTask(gOurPort, NULL);
-
-
- // create the sound objects we will use
- gIncomingSound = new CPPSound (128);
- gLogonSound = new CPPSound (129);
-
- // create the incoming message queue
- gTalkText = new CPPList();
-
- // create and open the application windows
- gSendWindow = new CPPSendWindow(gWindowManager);
- gMessageWindow = new CPPMessageWindow(gWindowManager);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPYenta::DoApplicationShutDown(void)
- /* close the communication port and get rid of the sound objects */
- /* The window manager will clean up our windows */
- {
- OSErr ErrCode;
-
- delete gSlaveDriver; // abort any outstanding tasks
-
- if ((ErrCode = gMaBell->CloseCommunicationPort(gOurPort)) != noErr)
- ErrorAlert (ErrCode, "\pCan't close communication port.");
-
- delete gMaBell; // close the appletalk manager
-
- // get rid of the sound objects
- delete gIncomingSound;
- delete gLogonSound;
- }
-
- /*-----------------------------------------------------------------*/
-
- Boolean CPPYenta::DoCommand (short commandID)
- {
- PrefsData OldPrefs;
- Str255 STemp;
-
- switch (commandID) {
- case kCmdAbout :
- DoAboutBox();
- break;
-
- case kCmdPrefs :
- OldPrefs = gPrefsInfo;
- if (DoPrefsWindow (&gPrefsInfo))
- {
- if (gPrefsInfo.scanRate != OldPrefs.scanRate)
- { // Dequeue the task if we don't want to scan anymore
- if (gPrefsInfo.scanRate <= 0)
- gSlaveDriver->RemovePeriodicTask(gStartScanZoneTask, TRUE);
- else
- {
- if (OldPrefs.scanRate == 0)
- { // going from no task to having one
- gStartScanZoneTask =
- new CPPSpawnZoneTask (gSlaveDriver,
- gPrefsInfo.scanRate * 3600,
- FALSE);
- gStartScanZoneTask->StartSpawnZoneTask (NULL);
- }
- else // simply change the rate
- gStartScanZoneTask->SetPeriod(gPrefsInfo.scanRate * 3600);
- }
- }
-
- if (gPrefsInfo.confirmRate != OldPrefs.confirmRate)
- {
- // Dequeue the task if we don't want the scan anymore
- if (gPrefsInfo.confirmRate <= 0)
- gSlaveDriver->RemovePeriodicTask(gStartConfirmUsersTask, TRUE);
- else
- {
- if (OldPrefs.confirmRate == 0)
- { // going from no task to having one
- gStartConfirmUsersTask =
- new CPPSpawnConfirmTask (gSlaveDriver,
- gPrefsInfo.confirmRate * 3600,
- FALSE);
- gStartConfirmUsersTask->StartSpawnConfirmTask (NULL);
- }
- else // simply change the rate
- gStartConfirmUsersTask->SetPeriod(gPrefsInfo.confirmRate * 3600);
- }
- }
- }
- break;
-
- case kCmdAutoReply:
- if (gReplyString)
- {
- DisposPtr((Ptr)gReplyString);
- gReplyString = NULL;
- gMenuBar->SetCommandText (kCmdAutoReply, "\pAutoReply On…");
- gSendWindow->RefreshItemStates();
- }
- else
- {
- if (DoRequest ("\pEnter the string to reply with",
- "\pOut of the office", &gReplyString))
- {
- // put the chevrons around the reply string
- PStrCat (255, STemp, 3, "\p«", gReplyString, "\p»");
- NukePtr(gReplyString);
- gReplyString = String2String (STemp);
-
- gMenuBar->SetCommandText (kCmdAutoReply, "\pAutoReply Off");
- gSendWindow->RefreshItemStates();
- }
- }
- break;
-
- case kCmdClose :
- gWindowManager->FrontWindowObject()->Close();
- break;
-
- case kCmdSend :
- if (gSendWindow)
- {
- if (!gSendWindow->WindowVisible())
- gSendWindow->Show (TRUE);
- SelectWindow(gSendWindow->GetWindow());
- }
- break;
-
- case kCmdMessage :
- if (gMessageWindow)
- {
- if (!gMessageWindow->WindowVisible())
- gMessageWindow->Show (TRUE);
- SelectWindow(gMessageWindow->GetWindow());
- }
- break;
-
- default:
- return CPPApplication::DoCommand(commandID);
- break;
- }
-
- return TRUE;
- }
-
-
- /*-----------------------------------------------------------------*/
-
- void CPPYenta::DoAppPeriodicTask (void)
- {
- // check to see if any messages have arrived
- if (gTalkText->GetNumItems() > 0)
- gSendWindow->ProcessMessageQ();
-
- // handle outstanding lookup calls
- gSlaveDriver->RunPeriodicTasks();
- }
-